home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6386 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  57 lines

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: automatic charrs
  5. Date: Sat, 24 Feb 1996 14:14:05 GMT
  6. Organization: Netcom
  7. Message-ID: <312f1cd3.224602571@nntp.ix.netcom.com>
  8. References: <4glp29$dsh@d2.tufts.edu>
  9. NNTP-Posting-Host: ix-dc11-01.ix.netcom.com
  10. X-NETCOM-Date: Sat Feb 24  6:13:32 AM PST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. rdorich@emerald.tufts.edu (Roberto Dorich) wrote:
  14.  
  15. > Hello,
  16. > Could someone enlighten me, and tell me why the following program runs
  17. > just dandy using Sun's cc (3.0.1), but crashes using gcc (?) ?
  18. > #include <stdio.h>
  19. > char *b() { return "Hello";}
  20. >  
  21. > main() {
  22. >   char *a=b();
  23. >  
  24. >   a[0]='X';
  25. >   printf("--> %s\n",a);
  26. > }
  27. > Esentially, gcc puts out the following assembly code:
  28. > call b,0
  29. > nop
  30. > mov 88,%o1        // %o1 = 'X'
  31. > stb %o1, [%o0]          // a[0] = %o1
  32. > And cc spits out the following:
  33. >         call    b
  34. >         nop
  35. >         mov     88,%l1
  36. >         stb     %l1,[%l0+0]
  37. > (this assembly code has been optimized by me :-/ )
  38. > Anyway, there isn't really that much difference... but I get a seg fault
  39. > with gcc.  Why?
  40.  
  41. Modifying literal strings is not supported by the C language.
  42. Attempting to do so results in undefined behavior.
  43.  
  44. Apparently gcc puts literal strings in read-only memory and cc does
  45. not.  Either is permissible.
  46.  
  47.  
  48. Michael M Rubenstein
  49.